<template>
  <view class="login-container">
    <!-- 装饰元素 - 背景渐变和气泡 -->
    <view class="bg-gradient-top"></view>
    <view class="bg-gradient-bottom"></view>
    <view class="bg-bubbles">
      <view class="bubble bubble-1 animate-float"></view>
      <view class="bubble bubble-2 animate-float" style="animation-delay: 0.5s;"></view>
      <view class="bubble bubble-3 animate-float" style="animation-delay: 1s;"></view>
      <view class="bubble bubble-4 animate-float" style="animation-delay: 1.5s;"></view>
      <view class="bubble bubble-5 animate-float" style="animation-delay: 2s;"></view>
    </view>
    
    <view class="login-header">
      <image class="logo animate-float" src="/static/icons/chat.png" mode="aspectFit"></image>
      <text class="app-name animate-float">伴学小星</text>
      <text class="slogan animate-float" style="animation-delay: 0.3s;">智能陪伴，快乐学习</text>
    </view>
    
    <view class="login-form soft-card transition-all hover-scale">
      <!-- 学号登录 -->
      <view class="student-login">
        <view class="input-group">
          <text class="label">用户名</text>
          <div class="input-wrapper">
            <uni-icons class="input-icon" type="person" size="24" color="#FF85B3"></uni-icons>
            <input class="input-soft" type="text" v-model="studentForm.username" placeholder="请输入用户名" />
          </div>
        </view>
        
        <view class="input-group">
          <text class="label">密码</text>
          <div class="input-wrapper">
            <uni-icons class="input-icon" type="locked" size="24" color="#FF85B3"></uni-icons>
            <input class="input-soft" type="password" v-model="studentForm.password" placeholder="请输入密码" />
          </div>
        </view>
        
        <button class="btn-soft login-btn" @click="studentLogin" :disabled="!canLogin" :class="{ 'animate-pulse': loading }">
          <uni-icons v-if="loading" type="spinner" size="24" color="#fff" class="icon-spin"></uni-icons>
          <text v-else>登录</text>
        </button>
        
        <view class="login-options">
          <text class="register transition-all hover-float" @click="register">新用户注册</text>
          <text class="forgot-password transition-all hover-float" @click="changePassword">忘记密码</text>
        </view>
      </view>
    </view>
    
    <view class="login-footer">
      <text class="version">版本 1.0.0</text>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { api } from '@/api';
import { storageAsync } from '@/utils';

const loginType = ref('student');
const studentForm = ref({
  username: '',
  password: ''
});
const loading = ref(false);
console.log('登录组件已加载');

// 计算是否可以登录
const canLogin = computed(() => {
  return studentForm.value.username.trim() && studentForm.value.password.trim();
});

// 登录前校验
const validateLogin = () => {
  if (!canLogin.value) {
    uni.showToast({ title: '请输入完整信息', icon: 'none' });
    return false;
  }
  return true;
};

// 登录
const studentLogin = async () => {
  if (!validateLogin()) {
    return;
  }
  
  loading.value = true;
  
  try {
    console.log('登录请求参数:', studentForm.value);
    const res = await api.auth.studentLogin(studentForm.value);
    
    console.log('登录响应:', res);
    
    // 保存登录信息
      if (res.data && res.data.token) {
        await storageAsync.set('token', res.data.token);
        // 修复字段名：使用user而不是userInfo
        await storageAsync.set('userInfo', res.data.user || {});
        
        console.log('登录信息保存成功，准备跳转');
        // 跳转到首页（对话页），确保不携带会话ID以开始新对话
        uni.switchTab({ 
          url: '/pages/chat/chat',  // 修改为tabBar中配置的页面
          fail: (err) => {
            console.error('跳转失败:', err);
          }
        });
        
        // 清除可能存在的临时会话ID存储
        try {
          uni.removeStorageSync('tempConversationId');
        } catch (e) {
          console.warn('清除临时会话ID失败:', e);
        }
      } else {
        throw new Error('无效的登录响应格式');
      }
  } catch (error) {
    console.error('登录失败', error);
    // 显示更详细的错误信息
    const errorMessage = error.response?.data?.message || error.message || '账号或密码错误';
    uni.showToast({ title: errorMessage, icon: 'none' });
  } finally {
    loading.value = false;
  }
};

// 注册
const register = () => {
  uni.navigateTo({
    url: '/pages/login/register'
  });
};

// 忘记密码
const changePassword = () => {
  uni.navigateTo({
    url: '/pages/login/changePassword'
  });
};

// 检查是否已登录（仅检查，不自动跳转）
const checkLoginStatus = async () => {
  try {
    const token = await storageAsync.get('token');
    if (token) {
      console.log('检测到已存在token，但需要用户手动登录');
      // 不再自动跳转，让用户手动点击登录按钮
    }
  } catch (error) {
    console.log('未登录或Token已过期');
  }
};

// 页面挂载时只检查登录状态，不执行跳转
onMounted(() => {
  console.log('登录页面已挂载');
});
</script>

<style scoped>
/* 全局变量 */
:root {
  --primary-color: #FF69B4;
  --primary-light: #FF85B3;
  --primary-dark: #FF4D94;
  --text-secondary: #FF85B3;
  --border-color: #FFD1E0;
}

.login-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: linear-gradient(135deg, #FFF5F7 0%, #FFE6F0 100%);
  padding: 60rpx 40rpx;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.bg-gradient-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 400rpx;
  background: linear-gradient(180deg, rgba(255, 182, 212, 0.3) 0%, rgba(255, 255, 255, 0) 100%);
  z-index: 0;
}

.bg-gradient-bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 400rpx;
  background: linear-gradient(0deg, rgba(255, 182, 212, 0.3) 0%, rgba(255, 255, 255, 0) 100%);
  z-index: 0;
}

.bg-bubbles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

.bubble {
  position: absolute;
  border-radius: 50%;
  opacity: 0.3;
  animation: floatSoft 8s ease-in-out infinite;
}

.bubble-1 {
  width: 300rpx;
  height: 300rpx;
  background: linear-gradient(135deg, rgba(255, 153, 204, 0.6) 0%, rgba(255, 182, 212, 0) 100%);
  top: -100rpx;
  right: -50rpx;
  animation-duration: 10s;
}

.bubble-2 {
  width: 200rpx;
  height: 200rpx;
  background: linear-gradient(135deg, rgba(255, 182, 212, 0.6) 0%, rgba(255, 153, 204, 0) 100%);
  bottom: 100rpx;
  left: -50rpx;
  animation-duration: 8s;
}

.bubble-3 {
  width: 150rpx;
  height: 150rpx;
  background: linear-gradient(135deg, rgba(255, 133, 179, 0.6) 0%, rgba(255, 153, 204, 0) 100%);
  top: 30%;
  left: -20rpx;
  animation-duration: 9s;
}

.bubble-4 {
  width: 100rpx;
  height: 100rpx;
  background: linear-gradient(135deg, rgba(255, 153, 204, 0.6) 0%, rgba(255, 133, 179, 0) 100%);
  bottom: 30%;
  right: 50rpx;
  animation-duration: 7s;
}

.bubble-5 {
  width: 180rpx;
  height: 180rpx;
  background: linear-gradient(135deg, rgba(255, 182, 212, 0.5) 0%, rgba(255, 133, 179, 0) 100%);
  top: 60%;
  right: 20%;
  animation-duration: 11s;
}

/* 登录头部 */
.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 60rpx;
  z-index: 1;
  margin-top: 40rpx;
}

.logo {
  width: 240rpx;
  height: 240rpx;
  margin-bottom: 30rpx;
  border-radius: 50rpx;
  background: linear-gradient(135deg, #fff 0%, #FFF5F7 100%);
  padding: 20rpx;
  box-shadow: 0 10rpx 30rpx rgba(255, 133, 162, 0.3);
  animation: floatSoft 6s ease-in-out infinite;
}

.app-name {
  font-size: 64rpx;
  font-weight: bold;
  color: #FF69B4;
  margin-bottom: 10rpx;
  text-shadow: 0 4rpx 8rpx rgba(255, 133, 162, 0.2);
  animation: floatSoft 5s ease-in-out infinite;
}

.slogan {
  font-size: 32rpx;
  color: #FF85B3;
  font-weight: 500;
  opacity: 0.9;
  animation: floatSoft 7s ease-in-out infinite;
}

/* 登录表单 */
.login-form {
  flex: 1;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 32rpx;
  padding: 50rpx;
  box-shadow: 0 15rpx 40rpx rgba(255, 133, 162, 0.2);
  z-index: 1;
  position: relative;
  max-width: 600rpx;
  align-self: center;
  width: 100%;
  backdrop-filter: blur(20rpx);
  border: 1rpx solid rgba(255, 182, 212, 0.3);
  animation: floatSoft 9s ease-in-out infinite;
}

.login-form::before {
  content: '';
  position: absolute;
  top: -2rpx;
  left: -2rpx;
  right: -2rpx;
  bottom: -2rpx;
  border-radius: 34rpx;
  background: linear-gradient(135deg, #FF69B4 0%, transparent 50%, transparent 50%, #FF85B3 100%);
  z-index: -1;
  opacity: 0.1;
  pointer-events: none;
}

/* 输入组 */
.input-group {
  margin-bottom: 40rpx;
  position: relative;
}

.label {
  display: block;
  font-size: 30rpx;
  color: #FF85B3;
  margin-bottom: 15rpx;
  font-weight: 600;
  opacity: 0.9;
}

.input-wrapper {
  position: relative;
  width: 100%;
}

.input-icon {
  position: absolute;
  left: 30rpx;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  transition: all 0.3s ease;
}

.input-soft {
  width: 100%;
  height: 92rpx;
  padding: 0 30rpx 0 80rpx;
  border: 2rpx solid #FFD1E0;
  border-radius: 46rpx;
  font-size: 30rpx;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.7);
  transition: all 0.3s ease-in-out;
  color: #333;
}

.input-wrapper:focus-within .input-icon {
  color: #FF69B4;
  transform: translateY(-50%) scale(1.1);
}

.input-soft:focus {
  border-color: #FF69B4;
  box-shadow: 0 0 0 8rpx rgba(255, 133, 162, 0.15);
  background-color: #fff;
}

.input-soft::placeholder {
  color: #FFB3D1;
  opacity: 0.8;
}

/* 登录按钮 */
.login-btn {
  background: linear-gradient(135deg, #FF69B4 0%, #FF85B3 100%);
  color: white;
  border-radius: 48rpx;
  height: 96rpx;
  line-height: 96rpx;
  font-size: 34rpx;
  margin-top: 50rpx;
  margin-bottom: 40rpx;
  font-weight: 600;
  border: none;
  box-shadow: 0 8rpx 24rpx rgba(255, 105, 180, 0.3);
  transition: all 0.3s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-btn:active::before {
  left: 100%;
}

.login-btn:active {
  transform: scale(0.98);
  box-shadow: 0 6rpx 16rpx rgba(255, 105, 180, 0.4);
}

.login-btn:disabled {
  background-color: #FFD6DC;
  background-image: none;
  box-shadow: none;
  transform: none;
  color: #FFB3D1;
}

.icon-spin {
  animation: spin 1s linear infinite;
}

/* 登录选项 */
.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 20rpx;
}

.register,
.forgot-password {
  font-size: 28rpx;
  color: #FF69B4;
  font-weight: 500;
  cursor: pointer;
  position: relative;
  padding: 10rpx 20rpx;
  transition: all 0.3s ease;
  border-radius: 20rpx;
}

.register:hover,
.forgot-password:hover {
  background-color: rgba(255, 133, 179, 0.1);
  transform: translateY(-2rpx);
}

.register::after,
.forgot-password::after {
  content: '';
  position: absolute;
  bottom: 5rpx;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 2rpx;
  background-color: #FF69B4;
  transition: width 0.3s ease-in-out;
}

.register:hover::after,
.forgot-password:hover::after {
  width: 80%;
}

/* 页脚 */
.login-footer {
  text-align: center;
  padding: 40rpx 0;
  z-index: 1;
}

.version {
  font-size: 24rpx;
  color: #FFB3D1;
  opacity: 0.8;
}

/* 动画定义 */
.animate-pulse {
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.02); }
}

@keyframes floatSoft {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20rpx) scale(1.02);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10rpx);
  }
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 交互效果 */
.hover-scale:active {
  transform: scale(0.98);
}

.hover-float:active {
  transform: translateY(2rpx);
}

.transition-all {
  transition: all 0.3s ease;
}

/* 软卡片效果 */
.soft-card {
  position: relative;
  overflow: hidden;
}

.soft-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2) 0%, transparent 50%, transparent 50%, rgba(255, 255, 255, 0.1) 100%);
  pointer-events: none;
}
</style>